package com.xsm.DataBase;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.TreeMap;

import com.sina.weibo.sdk.api.TextObject;
import com.tencent.b.b.t;
import com.xsm.object.CTextObject;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ReadMessage {
	SQLiteDatabase sqLiteDatabase;
	DataHelper helper;
	Context context;
	String userid;
	public ReadMessage(Context context,String userid){
		this.context=context;
		this.userid=userid;
		helper=new DataHelper(context,"XSM",null,1);
		sqLiteDatabase=helper.getReadableDatabase();
	}
	
	
	public ReadMessage(Context context){
		this.context=context;
		helper=new DataHelper(context,"XSM",null,1);
		sqLiteDatabase=helper.getReadableDatabase();
	}
	public void close(){
		helper.close();
		sqLiteDatabase.close();
	}
	
	public ArrayList<String> getMessage(){
		String sql1="select * from '"+userid+"Message' where userid='"+userid+"'";	
		Cursor cursor=sqLiteDatabase.rawQuery(sql1,null);
		ArrayList<String> s=new ArrayList<String>();
		try {
			s.clear();
			String level = "",phone = "",school = "",jindou = "",signature = "",name = "",sex="",icon="",email="";
			while (cursor.moveToNext()) {
				 level=cursor.getString(cursor.getColumnIndex("level"));
				phone=cursor.getString(cursor.getColumnIndex("issuephone"));
				 school=cursor.getString(cursor.getColumnIndex("school"));
				jindou=cursor.getString(cursor.getColumnIndex("jindou"));
				 signature=cursor.getString(cursor.getColumnIndex("signature"));
				 name=cursor.getString(cursor.getColumnIndex("name"));
				sex=cursor.getString(cursor.getColumnIndex("sex"));
				icon=cursor.getString(cursor.getColumnIndex("icon"));
				email=cursor.getString(cursor.getColumnIndex("email"));
				s.add(userid);
				s.add(level);
				s.add(name);
				s.add(sex);
				s.add(icon);
				s.add(school);
				s.add(jindou);
				s.add(phone);
				s.add(signature);
				s.add(email);
			}
			cursor.close();
			sqLiteDatabase.close();
			helper.close();
		} catch (Exception e) {
	    Log.d("my", e+"");
		}
		
		return s;	
	}
	
	public String getTypeString(String textid){
		String namea="",nameb="",namec="";
		try {
			String sql="select * from textbookc where textbookcid='"+textid+"'";
			Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
			while(cursor.moveToNext()){
				 namec=cursor.getString(cursor.getColumnIndex("namec"));
				String parentid=cursor.getString(cursor.getColumnIndex("parentid"));
				String gparentid=cursor.getString(cursor.getColumnIndex("gparentid"));
				String sql1="select nameb from textbookb where textbookbid='"+parentid+"'";
				Cursor cursor1=sqLiteDatabase.rawQuery(sql1,null);
				if(cursor1.moveToNext()){
				 nameb=cursor1.getString(cursor1.getColumnIndex("nameb"));
				 cursor1.close();
				}
				String sql2="select namea from textbooka where textbookaid='"+gparentid+"'";
				Cursor cursor2=sqLiteDatabase.rawQuery(sql2,null);
				if(cursor2.moveToNext()){
					 namea=cursor2.getString(cursor2.getColumnIndex("namea"));
					 cursor2.close();
				}
			}
		cursor.close();
		} catch (Exception e) {
      	Log.d("my", e+"");
		}
		sqLiteDatabase.close();
		helper.close();
		return namea+"-"+nameb+"-"+namec;	
	}

	public ArrayList<CTextObject> getTextObject(int c){
	ArrayList<CTextObject> arrayList=new ArrayList<CTextObject>();
	String tablename="";
	String name="";
	String textid="";
	switch (c) {
	case 1:
	tablename="textbooka";	
	name="namea";
	textid="textbookaid";
		break;
	case 2:
		tablename="textbookb";
		name="nameb";
		textid="textbookbid";
	break;
	case 3:
		tablename="textbookc";
		name="namec";
		textid="textbookcid";
		break;
	default:
		break;
	}
try {
	String sql="select * from '"+tablename+"' ";
	Cursor cursor=sqLiteDatabase.rawQuery(sql,null);
	while (cursor.moveToNext()) {
	String cname=cursor.getString(cursor.getColumnIndex(name));
	String ctextid=cursor.getString(cursor.getColumnIndex(textid));
	String cparentid=cursor.getString(cursor.getColumnIndex("parentid"));
	String cgparentid=cursor.getString(cursor.getColumnIndex("gparentid"));
	CTextObject object=new CTextObject();
	object.setGparentid(cgparentid);
	object.setName(cname);
	object.setParentid(cparentid);
	object.setTextid(ctextid);
	arrayList.add(object);
	}
	cursor.close();
} catch (Exception e) {
	Log.d("my", e+"");
}
sqLiteDatabase.close();
helper.close();
return arrayList;
	}
	
	
	public TreeMap<String,ArrayList<String>>  getrecommend(){
		TreeMap<String,ArrayList<String>> taHashtable=new TreeMap<String, ArrayList<String>>();
			String sql1="select * from Recommend order by rid asc";
            Cursor cursor2=sqLiteDatabase.rawQuery(sql1, null);
			while(cursor2.moveToNext()){
				String string=cursor2.getString(cursor2.getColumnIndex("classify"));
				String title=cursor2.getString(cursor2.getColumnIndex("gparentname"));
				if(taHashtable.get(title)==null)
				{
				ArrayList<String> arrayList=new ArrayList<String>();
				arrayList.add(string);
				taHashtable.put(title,arrayList);	
				}else {
				ArrayList<String> content=taHashtable.get(title);
				content.add(string);
				taHashtable.put(title, content);
				}
			}
			cursor2.close();
			sqLiteDatabase.close();
			return taHashtable;
	}
	 
	public String GetGparentId(String booktype){
		String sql="select gparentid from textbookc where textbookcid='"+booktype+"'";
	     Cursor cursor=sqLiteDatabase.rawQuery(sql, null);
	     if (cursor.moveToNext()) {
		 String gparentid=cursor.getString(cursor.getColumnIndex("gparentid"));
		 return gparentid;
	     }
	     cursor.close();
	     sqLiteDatabase.close();
	     helper.close();
	     return "1";
	}
}
